package wrapper

import (
	"fmt"
	"strings"
)

/**
 * description：
 * <p>
 * </p>
 * change history:
 * date                      defect#    person       comments
 * -------------------------------------------------------------<p>
 * 2023/9/25 16:11          ********    kong.ruo    create file.
 *
 * @author kong.ruo
 * @date 2023/9/25 16:11
 *
 * @since go 1.20
 */

type QueryWrapper struct {
	*BasicWrapper
	columns []string
}

func NewQueryWrapper() *QueryWrapper {
	return &QueryWrapper{
		BasicWrapper: NewBasicWrapper(),
	}
}

func (q *QueryWrapper) Select(columnList []string) *QueryWrapper {
	cols := make([]string, 0)
	for _, col := range columnList {
		cols = append(cols, col)
	}
	q.columns = cols
	return q
}

func (q *QueryWrapper) Build() string {
	queries := make([]string, 0)

	for _, qc := range q.queries {
		queries = append(queries, buildCondition(qc))
	}
	var cond strings.Builder
	if len(queries) != 0 {
		cond.WriteString(" WHERE ")
		cond.WriteString(joinConditions(queries, " AND "))
	}
	return fmt.Sprintf("SELECT %s FROM %s%s", FormatSlice(q.columns, ","), q.tableName, cond.String())
}
